// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services','ionic.closePopup', 'ngResource'])

.run(function($ionicPlatform, $rootScope,cfg,$http,$location,meilvFac,$ionicHistory) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });

  $rootScope.clearHistory = function(){
      $ionicHistory.clearHistory();
    }

  $rootScope.$on('$stateChangeStart',
  function(event, toState, toParams, fromState, fromParams){
      if(toState.hideTab){
        $rootScope.hideTab = true;
      }
      else{
        $rootScope.hideTab = false;
      }
  });

  $rootScope.fullPath = function(path){
    if(!path||path=='')
      return ' ';
    else if(path.indexOf('http://')!=-1||path.indexOf('https://')!=-1){
      return path;
    }else{
      return cfg.imgBaseUrl + path;
    }
  };

  $http({
          method: 'get',
          url: cfg.baseUrl+'home/wechat/getSignPackage'+'?url='+encodeURIComponent($location.absUrl().split('#')[0])
        }).success(function(data){
          wx.error(function(res){
            alert(JSON.stringify(res));
          });
          wx.config({
            debug: false,
            appId: data.appId,
            timestamp: parseInt(data.timestamp),
            nonceStr: data.nonceStr,
            signature: data.signature,
            jsApiList: ['openLocation'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
          }); 
        });

})
.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
  //tab position
  $ionicConfigProvider.platform.ios.tabs.style('standard');
  $ionicConfigProvider.platform.ios.tabs.position('bottom');
  $ionicConfigProvider.platform.android.tabs.style('standard');
  $ionicConfigProvider.platform.android.tabs.position('standard');

  $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
  $ionicConfigProvider.platform.android.navBar.alignTitle('left');

  $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
  $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');

  $ionicConfigProvider.platform.ios.views.transition('ios');
  $ionicConfigProvider.platform.android.views.transition('android');

  //title center
  $ionicConfigProvider.navBar.alignTitle('center');

  //scroll
  $ionicConfigProvider.scrolling.jsScrolling(true);
  
  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: '',
    abstract: true,
    templateUrl: 'templates/tabs.html'
  })

  // Each tab has its own nav history stack:

  .state('tab.home', {
    url: '/home',
    cache:false,
    views: {
      'tab-home': {
        templateUrl: 'templates/tab-home.html',
        controller: 'homeCtrl'
      }
    }
  })

  .state('tab.oneday', {
      url: '/oneday/:filter',
      cache:false,
      views: {
        'tab-oneday': {
          templateUrl: 'templates/tab-oneday.html',
          controller: 'onedayCtrl'
        }
      },
      params:{
        filter:null
      }
    })

  .state('tab.ticket', {
    url: '/ticket',
    views: {
      'tab-ticket': {
        templateUrl: 'templates/tab-ticket.html',
        controller: 'ticketCtrl'
      }
    }
  })
  .state('tab.orderticket', {
    url: '/orderticket/:type',
    hideTab:true,
    views: {
      'tab-ticket': {
        templateUrl: 'templates/order-ticket.html',
        controller: 'orderTicketCtrl'
      }
    }
  })
  .state('tab.ticketnext', {
    url: '/ticketnext/:type/:date/:num/:price',
    hideTab:true,
    views: {
      'tab-ticket': {
        templateUrl: 'templates/order-ticket-next.html',
        controller: 'ticketNextCtrl'
      }
    }
  })
  .state('tab.info', {
    url: '/info',
    cache:false,
    views: {
      'tab-info': {
        templateUrl: 'templates/tab-info.html',
        controller: 'myinfoCtrl'
      }
    }
  })
  .state('tab.orderstepone', {
    url: '/orderstepone/:id',
    hideTab:true,
    cache:true,
    views: {
      'tab-oneday': {
        templateUrl: 'templates/order-travel-first.html',
        controller: 'onedayOrderCtrl'
      }
    }
  })
  .state('tab.ordersteptwo', {
    url: '/ordersteptwo',
    hideTab:true,
    cache:true,
    views: {
      'tab-oneday': {
        templateUrl: 'templates/order-travel-second.html',
        controller: 'onedayOrderCtrl'
      }
    },
    params:{
      orderdetail:null
    }
  })
  .state('tab.infoself', {
    url: '/infoself',
    hideTab:true,
    views: {
      'tab-info': {
        templateUrl: 'templates/tab-info-self.html',
        controller: 'myinfoCtrl'
      }
    }
  })
  .state('tab.register', {
    url: '/register',
    hideTab:true,
    views: {
      'tab-info': {
        templateUrl: 'templates/tab-register.html',
        controller: 'registerCtrl'
      }
    }
  })
  .state('tab.onedaydetail', {
    url: '/onedaydetail/:id',
    hideTab:true,
    views: {
      'tab-oneday': {
        templateUrl: 'templates/onedaydetail.html',
        controller: 'onedaydetailCtrl'
      }
    },
    params:{
      id:null
    }
  })
  .state('tab.orderstate', {
    url: '/orderstate/:order',
    hideTab:true,
    cache:false,
    views: {
      'tab-info': {
        templateUrl: 'templates/orderstate.html',
        controller: 'orderstateCtrl'
      }
    }
  })
  .state('tab.userdetail', {
    url: '/userdetail/:id',
    hideTab:true,
    cache:false,
    views: {
      'tab-info': {
        templateUrl: 'templates/order-userdetail.html',
        controller: 'orderstateCtrl'
      }
    }
  })
  .state('tab.map', {
    url: '/map',
    hideTab:true,
    views: {
      'tab-ticket': {
        templateUrl: 'templates/map.html',
        controller: 'mapCtrl'
      }
    },
    params:{
      place:null
    }
  })
  .state('tab.pay', {
    url: '/pay/:order_id',
    hideTab:true,
    cache:false,
    views: {
      'tab-ticket': {
        templateUrl: 'templates/pay.html',
        controller: 'payCtrl'
      }
    }
  })
  .state('tab.mycollection', {
    url: '/mycollection',
    hideTab:true,
    cache:false,
    views: {
      'tab-info': {
        templateUrl: 'templates/mycollection.html',
        controller: 'mycollectionCtrl'
      }
    }
  })
   .state('tab.introduce', {
    url: '/introduce',
    hideTab:true,
    views: {
      'tab-ticket': {
        templateUrl: 'templates/introduce.html',
        controller: 'ticketCtrl'
      }
    },
    params:{
      spot:null,
      adult:null,
      child:null,
      older:null,
      booking:null,
      twoday:null,
      notify:null
    }
  })
    .state('tab.feedback', {
    url: '/feedback',
    hideTab:true,
    views: {
      'tab-info': {
        templateUrl: 'templates/feedback.html',
        controller: 'feedbackCtrl'
      }
    }
  })
    .state('tab.about', {
    url: '/about',
    hideTab:true,
    views: {
      'tab-info': {
        templateUrl: 'templates/about.html',
        controller: 'feedbackCtrl'
      }
    }
  })
;
  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/home');

})

.directive('fallbackSrc', function () {
    return{
        link: function postLink(scope, element, attrs) {
            element.bind('error', function () {
                angular.element(this).attr("src", attrs.fallbackSrc);
            });
        }
    }
})
